/**
 * 添加或者修改页面
 */
var TaskInfoDlg = {
    data: {
        plotData: "",
        startData: "",
        predictData: "",
        actualData: "",
        jiaodian: "",
    }
};

layui.use(['form', 'admin', 'ax', 'laydate', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var _delay = false;
    $("#btnSubmit").click(function () {
        if (_delay) {
            layer.msg('请勿多次点击提交！');
            return;
        };
        _delay = true;
        setTimeout(() => {
            _delay = false
        }, 2000);
        window.location.href = Feng.ctxPath + '/project';
        var list = [];
        $(".name").each(function (o, e) {
            var object = {};
            object.proId = $("#proId").val();
            object.taskId = $(e).next().find('.taskId').val();
            object.content = $(e).next().find('.msg').val();
            object.taskName = $(e).html();
            if (object.taskId == 33) {
                object.note = $("#note").val();
            }
            if ($(e).next().find('.msg').val() != null && $(e).next().find('.msg').val() != "") {
                list.push(object);
            }
        });
        $.ajax({
            type: "post",
            url: "/task/addRent",
            data: JSON.stringify(list),
            contentType: 'application/json',
            async: false,
            success: function () {
                list = [];
                layer.msg('提交成功！', {icon: 1},);
                $('#btnSubmit').hide()
            }, error: function () {
                list = [];
                window.location.reload();
            }
        });
    });

    //出图时间选择框
    laydate.render({
        elem: '#plotData',
        trigger: 'click',
        done: function (value, date, endDate) {
        }
    });

    //开工时间选择框
    laydate.render({
        elem: '#startData',
        trigger: 'click',
        done: function (value, date, endDate) {
            var date1 = new Date(value);
            var date2 = new Date(date1);
            date2.setDate(date1.getDate() + 28);
            var Y = date2.getFullYear() + '-';
            var M = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1) + '-';
            var D = date2.getDate();
            $("#predictData").val(Y + M + D);
        }
    });

    //实际时间选择框
    laydate.render({
        elem: '#actualData',
        trigger: 'click',
        done: function (value, date, endDate) {
        }
    });

    // 出图日期修改
    var _delay = false
    $('#31').click(function () {
        if (_delay) {
            layer.msg('请勿多次点击提交！');
            return;
        }
        ;
        _delay = true;
        setTimeout(() => {
            _delay = false
        }, 2000);
        var list = [];
        var object = {};
        object.proId = $("#proId").val();
        object.taskId = "31";
        object.content = $('#plotData').val();
        object.taskName = "出图日期";
        if ($('#plotData').val() != null && $('#plotData').val() != "") {
            list.push(object);
        }
            $.ajax({
                type: "post",
                url: "/task/editRent",
                data: JSON.stringify(list),
                contentType: 'application/json',
                async: false,
                success: function () {
                    list = [];
                    layer.msg('修改成功！', {icon: 1},);
                    window.location.reload();
                }, error: function () {
                    list = [];
                    window.location.reload();
                }
            });

    });

    // 开工日期修改
    var _delay = false
    $('#32').click(function () {
        if (_delay) {
            layer.msg('请勿多次点击提交！');
            return;
        };
        _delay = true;
        setTimeout(() => {
            _delay = false
        }, 2000);
        var list = [];
        var object = {};
        object.proId = $("#proId").val();
        object.taskId = "32";
        object.content = $('#startData').val();
        object.taskName = "开工日期";
        var object1 = {};
        object1.proId = $("#proId").val();
        object1.taskId = "33";
        object1.content = $('#predictData').val();
        object1.taskName = "预估完工日期";
        if ($('#predictData').val() != null && $('#predictData').val() != "") {
            list.push(object1);
        }if ($('#startData').val() != null && $('#startData').val() != "") {
            list.push(object);
        }
        $.ajax({
            type: "post",
            url: "/task/editRent",
            data: JSON.stringify(list),
            contentType: 'application/json',
            async: false,
            success: function () {
                list = [];
                layer.msg('修改成功！', {icon: 1},);
                window.location.reload();
            }, error: function () {
                list = [];
                window.location.reload();
            }
        });
    });

    // 交店日期修改
    var _delay = false
    $('#34').click(function () {
        if (_delay) {
            layer.msg('请勿多次点击提交！');
            return;
        }
        ;
        _delay = true;
        setTimeout(() => {
            _delay = false
        }, 2000);
        var list = [];
        var object = {};
        object.proId = $("#proId").val();
        object.taskId = "34";
        object.content = $('#actualData').val();
        object.taskName = "交店日期";
        if ($('#actualData').val() != null && $('#actualData').val() != "") {
            list.push(object);
        }
            $.ajax({
                type: "post",
                url: "/task/editRent",
                data: JSON.stringify(list),
                contentType: 'application/json',
                async: false,
                success: function () {
                    list = [];
                    layer.msg('修改成功！', {icon: 1},);
                    window.location.reload();
                }, error: function () {
                    list = [];
                    window.location.reload();
                }
            });

    });


    //交店时间选择框
    // laydate.render({
    //     elem: '#jiaodian',
    //     trigger: 'click',
    //     done: function (value, date, endDate) {
    //     }
    // });

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //现场踏勘
    $("#scouting").click(function (data) {
        window.location.href = Feng.ctxPath + '/project/premises?id=' + $("#proId").val();
    });

    //返回按钮
    $("#backupPage").click(function () {
        let _temp = admin.getTempData('indexTabs')
        let curr = 1
        _temp.map((item, i) => {
            if(item.menuId == '/project') {
                curr = _temp[i].currPage ? _temp[i].currPage : 1
            }
        })
        window.location.href = Feng.ctxPath + '/project#!fenye=' + curr
    });

    $("#note").blur(function () {
        if ($("#note").val() != null && $("#note").val() != undefined && $("#note").val() != "") {
            $("input[name=predictData]").attr("id", "predictData");
            var oDate1 = new Date();
            //alert(oDate1.toLocaleString().split(" ")[0].replaceAll('/','-'));
            var e = $("#predictData").val();
            var a = oDate1.toLocaleString().split(" ")[0].replaceAll('/', '-');
            if (new Date(a) > new Date(e) || $("#predictData").val() == "") {
                $("#predictData").removeAttr("disabled", "disabled");
                $("#predictData").removeAttr("readonly", "readonly");
                //预估完工时间选择框
                laydate.render({
                    elem: '#predictData',
                    trigger: 'click',
                    done: function (value, date, endDate) {
                    }
                });
            }
        } else {
            $("#predictData").attr("disabled", "disabled");
            $("#predictData").removeAttr('id');
        }
    });
});